package com.benjaminwan.read.free.config

import com.benjaminwan.read.free.R
import com.benjaminwan.read.free.base.viewmodel.state
import com.benjaminwan.read.free.model.ItemOption
import com.benjaminwan.read.free.model.action.Position
import com.benjaminwan.read.free.model.enums.Horizontal
import com.benjaminwan.read.free.model.enums.MouseButton
import com.benjaminwan.read.free.model.enums.Vertical
import com.benjaminwan.read.free.ui.main.MainViewModel
import com.benjaminwan.read.free.utils.getStringArray

object Options {
    inline fun <reified T> getAction(mainVM: MainViewModel, pos: Position): T? =
        if (pos.idGroup < 0 || pos.idAction < 0) {
            null
        } else {
            val state = mainVM.state()
            val action = state.script.groups[pos.idGroup].actions[pos.idAction]
            if (action is T) {
                action
            } else {
                null
            }
        }

    val optionVertical = Vertical.values().mapIndexed { index, vertical ->
        ItemOption(index, vertical.name)
    }

    val optionHorizontal = Horizontal.values().mapIndexed { index, horizontal ->
        ItemOption(index, horizontal.name)
    }

    val optionMouseButton = MouseButton.values().mapIndexed { index, mouseBtn ->
        ItemOption(index, mouseBtn.name)
    }

    val optionLoginQuestion =
        getStringArray(R.array.login_question_titles).mapIndexed { index, item ->
            ItemOption(index, item)
        }

}